package com.cy.hui_jie_dan.controller.user.app;

import com.cy.hui_jie_dan.comon.result.Result;
import com.cy.hui_jie_dan.popj.dto.userDTO;
import com.cy.hui_jie_dan.popj.dto.userLoginDTO;
import com.cy.hui_jie_dan.popj.entity.User;
import com.cy.hui_jie_dan.service.userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import com.cy.hui_jie_dan.comon.context.BaseContext;
import java.util.HashMap;

@RestController
@RequestMapping("/app/user")
@Slf4j
@Api(tags = "用户管理")
public class userController {
    @Autowired
    userService userService;
    @PostMapping("/getCaptcha")
    @ApiOperation("获取验证码")
    public Result<String> getacpcha(@RequestBody userLoginDTO userLoginDTO){
        log.info("获取验证码请求: {}", userLoginDTO);
        try {
            String captcha = userService.generate(userLoginDTO);
            log.info("验证码生成成功: {}", captcha);
            return Result.success(captcha);
        } catch (Exception e) {
            log.error("获取验证码失败", e);
            return Result.error("获取验证码失败: " + e.getMessage());
        }
    }
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<String> register(@RequestBody userLoginDTO userLoginDTO){

        boolean register = userService.register(userLoginDTO);
        if (register){
            return Result.success("注册成功");
        }
        return Result.error("注册失败");

    }
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<Map<String, Object>> login(@RequestBody userLoginDTO userLoginDTO){
        String token = userService.login(userLoginDTO);
        User user = userService.getUserByPhone(userLoginDTO.getPhoneNumber());
        Map<String, Object> response = new HashMap<>();
        response.put("token", token);
        response.put("userId", user.getUserId());
        return Result.success(response);
    }
    @PostMapping("/change")
    @ApiOperation("忘记密码")
    public Result<String> change(@RequestBody userLoginDTO userLoginDTO){
        boolean b =userService.change(userLoginDTO);
        if (!b){
            return Result.error("失败");
        }
        return Result.success("ok");
    }
    @PostMapping("/changePassword")
    @ApiOperation("修改密码")
    public Result<String> changePassword(@RequestBody userLoginDTO userLoginDTO){
        boolean i = userService.updataByPassWord(userLoginDTO);
        if (i) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
    @GetMapping("/list")
    @ApiOperation("获取所有用户（不包括自己）")
    public Result<List<Map<String, Object>>> listUsers() {
        log.info("开始获取用户列表");
        try {
            Long currentId = BaseContext.getCurrentId();
            if (currentId == null) {
                log.error("用户未登录或会话已过期");
                return Result.error("用户未登录或会话已过期，无法获取用户列表");
            }
            log.info("当前用户ID: {}", currentId);
            Integer myUserId = currentId.intValue();
            List<Map<String, Object>> users = userService.listOtherUsers(myUserId);
            log.info("成功获取用户列表，共 {} 个用户", users.size());
            return Result.success(users);
        } catch (Exception e) {
            log.error("获取用户列表失败", e);
            return Result.error("获取用户列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/nickname/{id}")
    public String getNickname(@PathVariable Integer id) {
        return userService.getNicknameById(id);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result<String> logout() {
        try {
            Long currentId = BaseContext.getCurrentId();
            if (currentId == null) {
                return Result.error("用户未登录");
            }
            userService.logout(currentId);
            return Result.success("退出成功");
        } catch (Exception e) {
            log.error("退出登录失败", e);
            return Result.error("退出失败: " + e.getMessage());
        }
    }
}
